<?php

function online_summy()
{
    global $curruser;
    global $conf;
    global $gametype;
    if ($curruser['acl'] == 255)
    {
        $rs = getdb("select count(distinct sid) from k_sessions where uid!=2");
        return $rs->f[0];
    }
    $acl = $curruser['acl'];
    $num = 0;
    $i = 1;
    while ($i < $acl)
    {
        $users = GetUserList($curruser['id'], $i);
        if ($users == "")
        {
            $andUserIN = " 0=1 AND ";
        }
        else
        {
            $andUserIN = " a.id IN ({$users}) AND ";
        }
        $sql = "SELECT count(distinct b.sid) FROM k_sessions b LEFT JOIN k_user a ON (b.uid = a.id) WHERE {$andUserIN} a.acl < {$acl} AND b.uid !=2";
        $rs = getdb($sql);
        $num += $rs->f[0];
        ++$i;
    }
    $rs->close();
    return $num;
}

function Second2Str($sec)
{
    $mm = intval($sec / 60);
    $ss = $sec % 60;
    if ($ss < 10)
    {
        $ss = "0" . $ss;
    }
    return $mm . ":" . $ss;
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if ($curruser['enabled'] != 1)
{
    exit();
}
else if ($curruser['acl'] == 255)
{
    $rs = getdb("select id from k_user where acl=5 and supe_account='" . $curruser['account'] . "' LIMIT 1");
    if (!$rs->eof)
    {
        exit();
    }
    if ($curruser['prev_id'] != 0 && !SubAccountRight($curruser, 2))
    {
        exit();
    }
}
else if (1 < $curruser['acl'])
{
    if (isset($_SESSION['subaccount']))
    {
        exit();
    }
}
else
{
    $user = new User();
    $user->logout();
    exit();
}
$gametype = intval(getvar("gametype"));
$gType = chkGametype($gametype);
switch ($gType)
{
    case 0 :
        $ui = new UI();
        break;
    default :
        include_once( "class_3d/CheckMode_3d.php" );
        $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
        $chk->checkShow3dPage();
        unset($chk);
        $ui = new UI_3D();
        $ui->header();
        break;
}
$numOnly = intval(getVar("numOnly"));
include_once( "ip/ip_fun.php" );
$ipfun=new QQWry();
if ($numOnly)
{
    ob_end_clean();
    sess_gc();
    echo online_summy();
    exit();
}
$orderByArr = array (0 => "a.account", 1 => "a.account", 2 => "b.ip");
$okAcl = $curruser['acl'] == 255 ? 5 : $curruser['acl'] - 1;
$orderBy = intval(getvar("orderBy"));
if (isset($orderByArr[$orderBy]))
{
    $orderByStr = $orderByArr[$orderBy];
}
else
{
    $orderByStr = "a.account";
}
$srcAcl = intval(getvar("srcAcl"));
if ($srcAcl < 1)
{
    $srcAcl = 1;
}
else if ($okAcl < $srcAcl)
{
    $srcAcl = $okAcl;
}
if ($curruser['acl'] == 255)
{
    $andUserIN = "";
}
else
{
    $users = GetUserList($curruser['id'], $srcAcl);
    if ($users == "")
    {
        $andUserIN = " 0=1 AND ";
    }
    else
    {
        $andUserIN = " a.id IN ({$users}) AND ";
    }
}
$member = getvar("member");
$page = intval(getvar("page"));
if ($page == 0)
{
    $page = 1;
}
if ($member != "")
{
    $sql = "SELECT a.id, a.account, a.name, a.acl, b.createtime, b.lastupdate, b.createtime, b.ip, b.uid ";
    $sql .= " FROM k_sessions b LEFT JOIN k_user a ON (b.uid = a.id) ";
    $sql .= " WHERE {$andUserIN} a.account = '{$member}' AND b.uid !=2";
}
else
{
    $sql = "SELECT a.id, a.account, a.name, a.acl, b.createtime, b.lastupdate, b.createtime, b.ip, b.uid ";
    $sql .= " FROM k_sessions b LEFT JOIN k_user a ON (b.uid = a.id) ";
    $sql .= " WHERE {$andUserIN} a.acl = {$srcAcl} AND b.uid !=2 ORDER BY {$orderByStr}";
}
$cmd = getvar("cmd");
if ($cmd == "")
{
    $cmd = "summy";
}
$rs = getdb($sql, $page, 20);
$tmp = "";
$i = 1;
if ($rs->eof)
{
    $tmp = "";
    $pageLink = "";
}
else
{
    $pageLink = $rs->pagelink();
}
$postTime = $conf->get("paper_post_bet");
$today = date("Hi") <= $postTime ? date("Y-m-d", strtotime("-1 day")) : date("Y-m-d");
if ($curruser['detailed_list'] == 0)
{
    $detailed_list_flag = false;
}
else
{
    $detailed_list_flag = true;
}
while (!$rs->eof)
{
    if (0 < $rs->f['uid'])
    {
        $acl = $rs->f['acl'];
        $id = $rs->f['id'];
        $tmp .= "<tr class=td2>";
        $tmp .= "<td>" . $i++;
        $tmp .= "<td nowrap>" . $aclname[$acl];
        $tmp .= "<td nowrap>" . $rs->f['account'];
        $tmp .= "<td nowrap>" . $rs->f['name'];
        if ($detailed_list_flag && $acl == 1)
        {
            $tmp .= "<td nowrap align=center><a href=\"javascript:paper({$id},1)\">全部</a></td>";
            $tmp .= "<td align=center><a href=\"javascript:paper({$id},0)\">" . $g_gametype_3d[$gametype]['chs'] . "注单</a></td>";
        }
        else
        {
            $tmp .= "<td>--</td><td>--</td>";
        }
        $tmp .= "<td>" . date("H:i:s", $rs->f['createtime']);
        $tmp .= "<td>" . date("H:i:s", $rs->f['lastupdate']);
        $tmp .= "<td>" . Second2Str(systime() - $rs->f['createtime']);
        if ($curruser['acl'] == 255)
        {
			$ipfun->QQWry($rs->f['ip']); 
			$area=$ipfun->Country;
            $tmp .= "<td>" . $rs->f['ip'].'('.$area.')'.'</td>';
        }
        else
        {
            $tmp .= "<td>********" . substr($rs->f['ip'], 0 - 4).'</td>';
        }
    }
    $rs->movenext();
}
$slcAcl = "";
$i = 1;
while ($i < 6)
{
    if ($i == $curruser['acl'])
    {
        break;
    }
    if ($srcAcl == $i)
    {
        $slcTemp = " selected ";
    }
    else
    {
        $slcTemp = "";
    }
    $slcAcl = "<option value=\"{$i}\" {$slcTemp} >{$aclname[$i]}</option>{$slcAcl}";
    ++$i;
}
$tpl = new Template2();
$tpl->load("online", "online_3d.htm");
$tpl->set(array (
    "list" => $tmp,
    "gametype" => $gametype,
    "slcAcl" => $slcAcl,
    "srcAcl" => $srcAcl,
    "orderBy" => $orderBy,
    "pageLink" => $pageLink,
    " selected ",
    "today" => $today,
    "member" => $member
));
$tpl->show("online");
$ui->footer();